/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package finder.filters;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alex
 */
public class LinkFilter extends RegexFilter
{
    public LinkFilter()
    {
        super("(?i)<\\s*a[^>]+href\\s*=\\s*[\"']([^\"']*)[\"']",1,true);
    }
    @Override
    public List<String> applyFilter(String url, String content) 
    {
        List<String> list = super.applyFilter(url, content);
        String[] urls = list.toArray(new String[0]);
        list.clear();
        for(int x =0; x < urls.length;x++)
        {
            try 
            {
                URI uri = new URI(urls[x]);
                if(uri.isAbsolute())
                    list.add(uri.toString());
                else
                    list.add(new URI(url).resolve(uri).toString());
            }
            catch (URISyntaxException ex) 
            {
                //Logger.getLogger(LinkFilter.class.getName()).log(Level.INFO, null, ex);
            }
        }
        return list;
    }
    @Override
    public void applyFilter(List<String> input) 
    {
        super.applyFilter(input);
    }
}
